package com.atguigui.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 1640.能否连接形成数组
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/9/22 8:42
 */
public class P1640CheckArrayFormationThroughConcatenation {
    public static void main(String[] args) {
        Solution solution = new P1640CheckArrayFormationThroughConcatenation().new Solution();
        // TO TEST
    }

    class Solution {
        public boolean canFormArray(int[] arr, int[][] pieces) {
            int n = arr.length, m = pieces.length;
            Map<Integer, Integer> index = new HashMap<Integer, Integer>();
            for (int i = 0; i < m; i++) {
                index.put(pieces[i][0], i);
            }
            for (int i = 0; i < n; ) {
                if (!index.containsKey(arr[i])) {
                    return false;
                }
                int j = index.get(arr[i]), len = pieces[j].length;
                for (int k = 0; k < len; k++) {
                    if (arr[i + k] != pieces[j][k]) {
                        return false;
                    }
                }
                i = i + len;
            }
            return true;
        }
    }
}
